<?php
require '\vendor\taobao\TopSdk.php';

class TmallController 
{	
	protected $appkey = '23038146';
	protected $secretKey = '4dd6883954417a28bfea022777c488a9';
	protected $sessionKey = '61002190f216567c65757a3d445186b1a0d6b9caadd0ea0352469034';
    protected $app;

    //
    public function topClient(){
    	date_default_timezone_set('Asia/Shanghai'); 
	    $c = new TopClient;
	    $c->appkey = $this->appkey;
	    $c->secretKey = $this->secretKey;
	    $sessionKey = $this->sessionKey;
	    return $c;
	}

	//授权类目及品牌获取 taobao.itemcats.authorize.get
	public function getAuthorize(){
		$req = new ItemcatsAuthorizeGetRequest;
		$req->setFields("brand.vid,brand.name,item_cat.cid");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//taobao.itemcats.get 根据获取的类目ID获取发布商品的叶子类目cid
	public function getCid($pid){
		$req = new ItemcatsGetRequest;
		// $req->setCids($cids);
		$req->setDatetime(date('Y-m-d h:i:s',time()));
		//$req->setFields("cid,parent_cid,name,is_parent");
		$req->setParentCid($pid);
		$resp = $this->topClient()->execute($req);
		return $resp;
	}

	//tmall.product.match.schema.get接口获取产品匹配的规则
	public function matchProductXml($cid){
		$req = new TmallProductMatchSchemaGetRequest;
		$req->setCategoryId($cid);
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//tmall.product.schema.match进行产品匹配 返回产品ID
	public function productMatch($cid,$xml){
		$req = new TmallProductSchemaMatchRequest;
		$req->setCategoryId($cid);
		$req->setPropvalues($xml);
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//tmall.product.add.schema.get接口获取产品发布涉及的规则
	public function getProductXml($cid){
		$req = new TmallProductAddSchemaGetRequest;
		$req->setCategoryId($cid);
		//$req->setBrandId("1");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//tmall.product.schema.add发布产品
	public function addProduct($cid,$xml){
		$req = new TmallProductSchemaAddRequest;
		$req->setCategoryId($cid);
		//$req->setBrandId("12345");
		$req->setXmlData($xml);
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//tmall.item.add.schema.get获取商品发布的规则
	public function getItemXml($cid,$product_id){
		$req = new TmallItemAddSchemaGetRequest;
		// $req = new TmallItemVipAddSchemaGetRequest;
		$req->setCategoryId($cid);
		$req->setProductId($product_id);
		$req->setType("b");
		$req->setIsvInit("false");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//tmall.item.schema.add进行商品上新，涉及图片上传时请使用taobao.picture.upload接口
	public function itemAdd($cid,$product_id,$xml){
		$req = new TmallItemSchemaAddRequest;
		$req->setCategoryId($cid);
		$req->setProductId($product_id);
		$req->setXmlData($xml);
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//图片上传
	public function upload($url,$name,$title){
		$req = new PictureUploadRequest;
		$req->setPictureCategoryId("0");
		//附件上传的机制参见PHP CURL文档，在文件路径前加@符号即可
		// $req->setImg("@D:/phpstudy/WWW/taobao/1.jpg");
		$req->setImg("@".$url);
		$req->setImageInputTitle($name);
		$req->setTitle($title);
		//$req->setPictureId("10000");
		$req->setClientType("client:computer");
		$req->setIsHttps("true");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//根据外部ID取商品
	public function getItem($outId){
		$req = new ItemsCustomGetRequest;
		$req->setOuterId($outId);
		$req->setFields("num_iid,sku,item_img,prop_img");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	//根据外部ID获取商品sku
	public function getSku($outId){
		$req = new SkusCustomGetRequest;
		$req->setOuterId($outId);
		$req->setFields("sku_id,properties");
		$resp = $this->topClient()->execute($req, $this->sessionKey);
		return $resp;
	}

	public function test(){
		$a = $this->getItemXml('50014075','842415233');
		print_r($a);
	}


}